<?php

include_once('ConnectionManager.php');

function selectAll() {
    $query = "SELECT * FROM RECRUITMENT ORDER BY REC_CONTENT ASC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countByName($keyword) {
    $select = "SELECT REC_ID ";
    $from = "FROM RECRUITMENT ";
    $where = "WHERE REC_CONTENT LIKE '" . $keyword . "'";

    ConnectionManager::getInstance()->setQuery($select . $from . $where);
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->getNumRows();
}

function delete($id) {
    $sql = "DELETE FROM RECRUITMENT WHERE REC_ID = " . $id;
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function loadRecruitmentByID($recID) {
    $query = "SELECT * FROM RECRUITMENT WHERE REC_ID = $recID";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countTotalRecruitment() {
    $query = "SELECT REC_ID FROM RECRUITMENT";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->getNumRows();
}

function update($recID, $name) {
    $sql = "UPDATE RECRUITMENT 
                SET REC_CONTENT = '$name'
                WHERE REC_ID = $recID";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function insert($name) {
    $sql = "INSERT INTO RECRUITMENT (REC_CONTENT) 
                VALUES ('$name')";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function findBy($keyword, $start_position, $row_per_page) {
    $select = "SELECT REC_ID, REC_CONTENT FROM RECRUITMENT ";
    $where = "";


    if (isset($keyword)) {
        $where .= "WHERE REC_CONTENT LIKE '%$keyword%'";
    }

    $where .= " LIMIT " . $start_position . ", " . $row_per_page;
    ConnectionManager::getInstance()->setQuery($select . $where);
    return ConnectionManager::getInstance()->query();
}

function findFirstRowOfRecruitment(){
	$sql = "SELECT * FROM RECRUITMENT LIMIT 0, 1";
	ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

?>